﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MonitorPlan.Admin
{
    public partial class Contact : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["userid"] == null)
                {
                    Response.Redirect("/Admin");
                }
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            this.rp1.DataBind();
            this.mv.ActiveViewIndex = 0;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 1;
            this.txtContact.Text = "";
            this.actionSms.InnerHtml = "";
        }

        protected void rp1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            // delete a contact when you user click on delete button in repeater.
            if (e.CommandName == "delete")
            {
                string id = e.CommandArgument.ToString();
                Model.Contact cont = new Model.Contact();
                if(cont.Remove(id))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
            }
            if(e.CommandName =="edit")
            {
                string id = e.CommandArgument.ToString();
                this.mv.ActiveViewIndex = 2;
                Model.Contact con = new Model.Contact().GetContact(id);
                this.ddlConType.SelectedIndex = this.ddlConType.Items.IndexOf(this.ddlConType.Items.FindByText(con.ContactType));
                this.ddlUserName.SelectedValue = con.UserId.ToString();
                this.txtCon.Text = con.Name;
                this.txtid.Value = id;
            }
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
            this.rp1.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            this.txtContact.Text = "";
            this.actionSms.InnerHtml = "";
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if(this.txtContact.Text!="")
            {
                Model.Contact con = new Model.Contact();
                con.UserId = Convert.ToInt32(this.ddlUser.SelectedValue);
                con.Name = this.txtContact.Text;
                con.ContactType = this.ddlContactType.SelectedValue;
                string sms;
                if(con.Add())
                {
                    sms = "<div class='alert alert-success'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has been save successfully!";
                    sms += "</div>";
                    this.txtContact.Text = "";
                    this.actionSms.InnerHtml = sms;
                }
                else
                {
                    sms = "<div class='alert alert-error'>";
                    sms += "<button type='button' class='close' data-dismiss='alert'>&times;</button>";
                    sms += "Data has not been saved successfully. Try again!";
                    sms += "</div>";
                    this.actionSms.InnerHtml = sms;
                }
            }
            else
            {
                this.actionSms.InnerHtml = "<span style='color:red'>Please input a contact!</span>";
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            if(this.txtCon.Text!="")
            {
                Model.Contact con = new Model.Contact();
                con.Name = this.txtCon.Text;
                con.UserId = Convert.ToInt32(this.ddlUserName.SelectedValue);
                con.ContactType = this.ddlConType.SelectedValue;
                con.Id = Convert.ToInt32(this.txtid.Value);
                if(con.Update(this.txtid.Value))
                {
                    this.rp1.DataBind();
                    this.mv.ActiveViewIndex = 0;
                }
                else
                {
                    this.updateSms.InnerHtml = "Cannot update data. Try again!";
                }
            }
        }

        protected void ddlUserName_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void btnCancelUpdate_Click(object sender, EventArgs e)
        {
            this.mv.ActiveViewIndex = 0;
        }
    }
}